package com.example.java.proxy;

import java.lang.reflect.Method;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronizationAdapter;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.transaction.support.TransactionSynchronizationUtils;

@Transactional
public class CGLibProxy implements MethodInterceptor {

	private Object targetObject;// CGLib需要代理的目标对象

	public Object createProxyObject(Object obj) {
		
		TransactionSynchronizationManager.bindResource(null, null);
		DataSourceTransactionManager sssss = new DataSourceTransactionManager();
		this.targetObject = obj;
		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(obj.getClass());
		enhancer.setCallback(this);
		Object proxyObj = enhancer.create();
		return proxyObj;// 返回代理对象
	}

	@Override
	public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
		Object obj = null;
		if ("addUser".equals(method.getName())) {// 过滤方法
			checkPopedom();// 检查权限
		}
		obj = method.invoke(targetObject, args);
		return obj;
	}

	private void checkPopedom() {
		System.out.println(".:检查权限  checkPopedom()!");
	}
}
